package Assignment11_SnakeNLadders;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Assignment11_SnakeNLadders {

	static int sumA = 0;
	static int sumB = 0;

	public static void main(String[] args) {

		Map<Integer, Integer> hm_sl = new HashMap<Integer, Integer>();

		Scanner sc = new Scanner(System.in);

		String str = sc.nextLine();
		Scanner sc1 = new Scanner(str);
		int no_snl = sc1.nextInt();
		System.out.println(no_snl);

		while (sc.hasNextLine()) {

			for (int i = 0; i < no_snl; i++) {

				String str1 = sc.nextLine();
				// System.out.println("Snake and Ladder cells :: " + str1);
				Scanner sc11 = new Scanner(str1);

				hm_sl.put(sc11.nextInt(), sc11.nextInt());

			}
			System.out.println(hm_sl);

			// Number of Moves
			String str2 = sc.nextLine();
			int no_moves = Integer.parseInt(str2);

			String str3 = sc.nextLine();
			// System.out.println("Actual Moves :: " + str3);

			// int moves_byA[] = new int[no_moves / 2];
			// int moves_byB[] = new int[no_moves / 2];

			Scanner scnum = new Scanner(str3);

			for (int i = 0; i < no_moves; i++) {

				// getting the moves for player A
				if (i % 2 == 0) {

					int I = scnum.nextInt();
					// System.out.println("Player A :: " + I);

					sumA = sumA + I;

					while (hm_sl.containsKey(sumA)) {
						sumA = hm_sl.get(sumA);

					}

					System.out.println("A " + sumA);
					
					if (sumA >= 99) {
						System.out.println("A " + sumA);
						break;
					}
				}

				// getting the moves for player B
				else {
					int I = scnum.nextInt();

					sumB = sumB + I;

					while (hm_sl.containsKey(sumB)) {
						sumB = hm_sl.get(sumB);
					}
					System.out.println("B " + sumB);
					
					if (sumB >= 99) {
						System.out.println("B " + sumB);
						break;
					}

				}

			}

			System.out.println("sumA :: " + sumA + " sumB :: " + sumB);

			if (sumA > sumB) {
				System.out.println("A " + sumA);
			} else {
				System.out.println("B " + sumB);
			}

		}

	}
}
